package com.apestech.framework.db;

import com.apestech.framework.util.DateUtil;
import com.apestech.framework.xml.sql.modul.QueryhTw;
import com.apestech.framework.util.FormatUtil;

import java.sql.Clob;
import java.sql.Timestamp;

/**
 * resultset里每个字段的格式化处理，需要外面传入格式化相关命令，类型为JSONObject
 *
 * @author hup
 * @create 2018-06-06-9:36
 */
public class FieldFormat extends FormatUtil{

    public class XLSField {
        private Object formatValue;
        private String pattern;

        public XLSField(String pattern, Object formatValue){
            this.pattern=pattern;
            this.formatValue=formatValue;
        }

        public String getPattern() {
            return pattern;
        }

        public Object getFormatValue() {
            return formatValue;
        }
    }

    private Format format;
    private boolean xls;
    private boolean bmath;
    private String timestampField;

    public void setFormat(Format format, boolean xls, boolean bmath) throws Exception {
        this.format=format;
        this.xls=xls;
        this.bmath=bmath;
    }

    public void setTimestampField(String timestampField) {
        this.timestampField = timestampField;
    }

    public XLSField getXLSField(String field, Object value) throws Exception {
        if(format==null) return null;
        FieldFormatInfo f=new FieldFormatInfo(format.getFieldById(field), value, xls, bmath);
        return new XLSField(f.getPattern(), f.getFormatValue());
    }

    public Object getValue(String field, Object value, QueryhTw htw) throws Exception {
        if(value==null) return null;
        String sclass=value.getClass().getName().toUpperCase();
        if(format==null){
            if(sclass.indexOf("TIMESTAMP")>=0){
                if(timestampField!=null && timestampField.equals(field) && value instanceof oracle.sql.TIMESTAMP)
                    return DateUtil.getTimeStampStr(((oracle.sql.TIMESTAMP)value).timestampValue());
                else{
                    if(value instanceof java.sql.Timestamp){
                        value=castToDate(value);
                        return value;
                    } else {
                        oracle.sql.TIMESTAMP time=(oracle.sql.TIMESTAMP)value;
                        value=new Timestamp(time.timestampValue().getTime());
                        return value;
                    }
                }
            }
            else if(sclass.indexOf("CLOB")>=0) return castClobToString((Clob) value);
            return value;
        }
        if(htw!=null) field=htw.getPivotOldField(field);
        FieldFormatInfo f=new FieldFormatInfo(format.getFieldById(field), value, xls, bmath);
        Object v=f.getFormatValue();
        if(v==null && value!=null){
            if(value instanceof oracle.sql.TIMESTAMP){
                oracle.sql.TIMESTAMP time=(oracle.sql.TIMESTAMP)value;
                v=new Timestamp(time.timestampValue().getTime());
            } else  if(sclass.indexOf("CLOB")>=0)
                v=castClobToString((Clob) value);
            else v=value;
        }
        return v;
    }

}
